//
//  LGTHomeTaskList.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/10.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
import ZHRefresh

class LGTHomeTaskList: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{
    
    enum DataType:Int{
        case Simple = 0
        case High = 1
    }
    
    var tableView:UITableView?
    var headView:LGTHomeHeaderView?
    var data:Array<TaskModel> = []
    var adsArray:Array<Dictionary<String, Any>> = []
    var page:Int = 1
    var dataType:DataType = .Simple
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.setBlock()
    }

    func setBlock(){
        weak var weakSelf = self
        self.reloadBlock = {() in
            weakSelf?.tableView?.header?.beginRefreshing()
        }
    }
    
    override func setUI() {
        super.setUI()
        var title:String = "简单任务"
        if self.dataType == .High{
            title = "高价任务"
        }
        self.setBavBarStyle(navBarHiden: false, title: title, back: true)
        self.tableView = UITableView.init(frame: CGRect.init(x: 0.0, y: NavBarH(), width: Double(KScreenW), height: Double(KScreenH) - NavBarH()), style: UITableView.Style.plain)
        self.view.addSubview(self.tableView!)
        self.tableView?.backgroundColor = ViewBackColor
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.register(LGTHomeCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        weak var weakSelf = self
        //ZHRefreshNormalHeader.headerWithRefreshing
        //ZHRefreshHeader.headerWithRefreshing
        self.tableView?.header = ZHRefreshNormalHeader.headerWithRefreshing(block: {
            weakSelf?.page = 1
            weakSelf?.tableView?.footer?.endRefreshing()
            weakSelf?.getData()
        })
        
        self.tableView?.footer = ZHRefreshBackNormalFooter.footerWithRefreshing(block: {
            weakSelf?.page += 1
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.getData()
        })
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTHomeCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTHomeCell
        cell.model = self.data[indexPath.row]
        return cell
    }
    
    func getData(){
        weak var weakSelf = self
        var param:Dictionary<String, Any> = ["page": self.page]
        if self.dataType == .Simple{
            param["is_simple"] = 1
        }else if self.dataType == .High{
            param["is_high_price"] = 1
        }
        
        LGTHttpUtil.httpUtil.requestData(url: LLZ_Task_List_URL, param: param, showMessage: false, success: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
            let data:Array<Dictionary<String, Any>> =  json["data"] as? Array<Dictionary<String, Any>> ?? Array.init()
            var modeArray:Array<TaskModel> = Array.init()
            for i in 0..<data.count{
                let param:Dictionary<String, Any> = data[i]
                let model:TaskModel = try! lgtDecode(TaskModel.self, param: param)
                modeArray.append(model)
            }
            
            if weakSelf?.page == 1{
                weakSelf?.data = modeArray
                
            }else{
                if weakSelf?.data == nil{
                    weakSelf?.data = Array.init()
                }
                weakSelf?.data.append(contentsOf: modeArray)
            }
            
            if weakSelf?.data.count == 0{
                weakSelf?.showNoData(image: NoData_Image, text: NoData_Text, subText: "")
            }
            
            if modeArray.count == 0{
                weakSelf?.tableView?.footer?.endRefreshingWithNoMoreData()
            }else{
                weakSelf?.tableView?.footer?.resetNoMoreData()
            }
            weakSelf?.tableView?.reloadData()
        }, failure: { (json) in
            weakSelf?.tableView?.header?.endRefreshing()
            weakSelf?.tableView?.footer?.endRefreshing()
        })
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let detailVC:LGTPostDetailVC = LGTPostDetailVC()
        detailVC.viewDetailType = DetailType.LLZ_Task_Detail_Type
        detailVC.taskModel = self.data[indexPath.row]
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.tableView?.header?.beginRefreshing()
    }
}
